package BinarySearch;

public class mySqrt {
    public int mySqrt(int x) {
        return (int) Math.floor(Math.sqrt(x));
    }

    public int mySqrtI(int x) {
        if (x <= 1) {
            return x;
        }
        int l = 2;
        int h = x / 2;
        int res;
        while (l <= h) {
            int middle = l + (h - l) / 2;
            int num = middle * middle;
            if (num > x) {
                h = middle - 1;
            } else if (num < x) {
                l = middle + 1;
            } else {
                return middle;
            }
        }
        return h;
    }
}
